/**
 * 
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Article;
import domain.Computer;
import domain.Laptop;
import domain.Product;

/**
 * @author grupo22
 *
 */
public class JDBCComputerDAO implements IComputerDAO {

	/* (non-Javadoc)
	 * @see data.IComputerDAO#select(java.sql.Connection, java.lang.String, domain.Computer)
	 */
	@Override
	public Computer select(Connection con, String oid, Computer c) {
		String sql = "SELECT * FROM Computer WHERE (OIDProduct = ? )";
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);

            rs = stmt.executeQuery();

            rs.next();
            c.setProcessor(rs.getString("processor"));
            c.setMemory(rs.getInt("memory"));
            c.setDisc(rs.getInt("disc"));
    		c.setDisplay(rs.getDouble("display"));
    		c.setGuadalinexComp(rs.getBoolean("guadalinexComp"));
    		if (rs.getString("objectType").compareTo("laptop")==0){
    			Laptop l = new Laptop(c.getBrand(),c.getModel(),c.getPrice(),c.getArticle(),c.getSupplier(),c.getProcessor(),c.getMemory(),c.getDisc(),c.getDisplay(),c.isGuadalinexComp(),rs.getInt("autonomy"),rs.getDouble("weight"));
    			c = l;
    		}
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
        return c;
	}

	@Override
	public void insert(Connection con, Computer c, String oid) {
		String sql = "INSERT INTO Computer (OIDProduct, processor, memory, disc, display, guadalinexComp, objectType) VALUES (?,?,?,?,?,?,?)";
        PreparedStatement stmt = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);
            stmt.setString(2, c.getProcessor());
            stmt.setInt(3, c.getMemory());
            stmt.setInt(4, c.getDisc());
            stmt.setDouble(5, c.getDisplay());
            stmt.setBoolean(6, c.isGuadalinexComp());
            stmt.setString(7, "computer");

            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
	}
	
	@Override
	public void insert(Connection con, Laptop l, String oid) {
		String sql = "INSERT INTO Computer (OIDProduct, processor, memory, disc, display, guadalinexComp, objectType, autonomy, weight) VALUES (?,?,?,?,?,?,?,?,?)";
        PreparedStatement stmt = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);
            stmt.setString(2, l.getProcessor());
            stmt.setInt(3, l.getMemory());
            stmt.setInt(4, l.getDisc());
            stmt.setDouble(5, l.getDisplay());
            stmt.setBoolean(6, l.isGuadalinexComp());
            stmt.setString(7, "laptop");
            stmt.setInt(8, l.getAutonomy());
            stmt.setDouble(9, l.getWeight());

            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
	}
}
